<template>
  <div>
    <div
      v-show="showIndex === 0"
      class="app-container"
    >
      <div class="header-search">
        <div class="item-other">
          <el-button
            class="search-button"
            size="mini"
            type="success"
            @click="addLine"
          >添加轮播图
          </el-button>
        </div>
        <div class="item-other">
          <el-button
            class="search-button"
            size="mini"
            type="success"
            @click="reflesh"
          >刷新
          </el-button>
        </div>
      </div>
      <div
        v-loading="loading"
        style="min-height: 620px"
      >
        <el-table
          v-show="!loading"
          ref="TeaList"
          :data="tableData"
          height="620"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
          />
          <el-table-column
            label="轮播图id"
            prop="id"
          />
          <el-table-column
            label="轮播图跳转链接"
            prop="link"
          />
          <el-table-column
            label="轮播图预览"
            prop="path"
          >
            <template slot-scope="scope">
              <div
                style="
                  display: flex;
                  justify-content: center;
                  width: 100px;
                  height: 100px;
                "
              >
                <!-- <img :src="url + scope.row['path']" /> -->
                <el-image
                  style=""
                  :src="url + scope.row['path']"
                  :preview-src-list="[url + scope.row['path']]"
                />
              </div>
            </template>
          </el-table-column>
          <el-table-column
            width="300"
            align="center"
            label="操作"
          >
            <template slot-scope="scope">
              <div style="display: flex; justify-content: center">
                <el-button
                  size="mini"
                  type="primary"
                  @click="handleUpdate(scope.$index, scope.row)"
                >
                  修改
                </el-button>
                <el-button
                  size="mini"
                  type="danger"
                  @click="handleDelete(scope.$index, scope.row)"
                >
                  删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <add-view
      v-if="showIndex === 1"
      :show-index.sync="showIndex"
      @goBack="goBack"
    />
    <update-view
      v-if="showIndex === 2"
      :id="updateId"
      :show-index.sync="showIndex"
      @goBack="goBack"
    />

    <el-backtop :visibility-height="10" />
  </div>
</template>

<script>
import { deleteCarousel, carouselListSelect } from '@/api/carousels';
import { Loading } from 'element-ui';
import AddView from './view/create.vue';
import UpdateView from './view/update.vue';
import { mapState } from 'vuex';
export default {
  name: 'List',
  components: {
    AddView,
    UpdateView
  },
  filters: {
    getRealTime(e) {
      console.log(e);
      if (e) {
        const time = new Date(e);
        const year = time.getFullYear();
        const _month = time.getMonth() + 1;
        const month = _month < 10 ? '0' + _month : _month;
        const day = time.getDay() < 10 ? '0' + time.getDay() : time.getDay();
        const hours =
          time.getHours() < 10 ? '0' + time.getHours() : time.getHours();
        const min =
          time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes();
        const second =
          time.getDate() < 10 ? '0' + time.getDate() : time.getDate();
        return (
          year +
          '-' +
          month +
          '-' +
          day +
          ' ' +
          hours +
          ':' +
          min +
          ':' +
          second
        );
      }
      return '';
    }
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 20,
      totalSize: 0,
      totalPages: 0,
      tableData: [],
      value: '',
      select: [],
      showIndex: 0,
      loading: false,
      updateId: 0
    };
  },
  created() {
    this.geList();
  },
  computed: {
    ...mapState({
      url: (state) => state.url
    })
  },
  methods: {
    goBack() {
      this.loading = true;
      this.geList(() => {
        setTimeout((res) => {
          this.loading = false;
        }, 1000);
      });
    },
    addLine() {
      this.showIndex = 1;
    },
    reflesh() {
      this.loading = true;
      this.geList(() => {
        setTimeout((res) => {
          this.loading = false;
        }, 1000);
      });
    },
    handleUpdate(index, row) {
      console.log(row['id']);
      this.updateId = row['id'];
      this.showIndex = 2;
    },
    handleSelectionChange(e) {
      this.select = e.map((item) => item.id);
    },
    geList(callback) {
      carouselListSelect({
        page: this.pageNum,
        size: this.pageSize
      })
        .then((res) => {
          console.log(res);
          callback && callback();
          this.tableData = res.data.data;
        })
        .catch((err) => {
          callback && callback();
          console.log(err);
        });
    },
    searchValue() {
      // if (!this.selectInput) return this.$message.info('请先选择类别');
      // if (!this.search) return this.$message.info('请输入搜索内容');
      this.pageNum = 1;
      this.geList();
    },
    handleEdit(index, row) {
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
      this.$confirm('此操作将永久删除该轮播图, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          const loadingInstance = Loading.service({
            fullscreen: true
          });
          deleteCarousel([row['id']])
            .then((res) => {
              if (res.data.code === 200) {
                this.geList();
                loadingInstance.close();
                this.$message.success('删除成功');
                return;
              }
              this.geList();
              loadingInstance.close();
              this.$message.error(res.data.message);
            })
            .catch((err) => {
              console.log(err);
              loadingInstance.close();
              this.$message.error('删除失败');
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
    },
    change(e) {
      this.pageNum = e;
      this.geList();
    },
    getSomeQrCode() {}
  }
};
</script>

<style lang="scss" scoped>
.app-container {
  height: 730px;
  overflow: hidden;
  .header-search {
    display: flex;
    // width: 500px;
    > .header-search-title {
      width: 100px;
      color: #909399;
      font-size: 16px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    > .item {
      display: flex;
      overflow: hidden;
      width: 650px;
      justify-content: space-between;
      > .search-select {
        width: 350px;
        margin-right: 4px;
      }
      > .search-input {
        //width: 60px;
      }
      > .search-button {
        margin-left: 4px;
      }
    }

    > .item-other {
      margin-left: 20px;
    }
  }
  .footer-pagination {
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
